package org.aaron.wbclient.oauth;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;

import org.aaron.wbclient.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PrepareRequestActivity extends Activity {

	private OAuthConsumer oAuthConsumer;
	private OAuthProvider oAuthProvider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		oAuthConsumer = new CommonsHttpOAuthConsumer(Constans.CONSUMER_KEY, Constans.CONSUMER_SECRET);
		oAuthProvider = new CommonsHttpOAuthProvider(Constans.REQUEST_TOKEN_URL, Constans.ACCESS_TOKEN_URL, Constans.AUTHORIZE_URL);
		new OAuthRequestTokenTask(oAuthConsumer, oAuthProvider, PrepareRequestActivity.this).execute();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
		final Uri uri = intent.getData();
		if (uri != null && Constans.OAUTH_CALLBACK_SCHEME.equals(uri.getScheme())) {
			Log.d("retrieveRequest retrun uri:", uri + "");
			new RetrieveAccessTokenTask(oAuthConsumer, oAuthProvider, this, preferences).execute(uri);
			finish();
		}
	}
}
